/*
Authors: Eli Yucht 300388949 and Lior Fass 300069705
Project: SCP (Switch Schedule Policy)
Description: RR Scheduler class implementation
*/

#include "RR_Scheduler.h"

/* This function implements the logic behind RR Scheduler,
	which means choosing the first packet of the current flow and moving to the next flow.
	steps:
	1. Take out first packet from current flow's head packets queue
		(if the queue is not empty,  else continue to the next (successive) flow)
	2. Write to the output file the result
	3. Advance the current_time (to the time after this packet was sent),
	4. Load from the input file (if not reached to the end of file yet) 
		all the new packets that arrived to the system until that time.

	On success this function return 0, on fail return 1.
*/

int RR_Scheduler::ScheduleCurrentFlow()
{
	long int return_time = current_flow->Remove_Pkt_From_Flow(current_time, outfile_p);
	if (return_time == -1)
	{
		cerr << "Failed to remove packet from flow.\n";
		return 1;
	}
	else
	{
		current_time = return_time;
		if (!end_of_input)
		{
			if (Load_data_to_Schedule())
			{
				cerr << "Failed to load data from file.\n";
				return 1;
			}
		}
	}
	return 0;
}